package pers.hyj.ims.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author H_Yunjia
 */

public class LoginView extends JFrame{

    public LoginView(){
        super();
        setTitle("登录");
        setLocation(450,300);
        setSize(400,250);
        setResizable(false);
        initLogin();
    }

    //组件    desc="内容"
    //<editor-fold> defaultstate="collapsed"
    //主容器
    private JPanel jPanel_main = new JPanel();

    //占位容器
    private JPanel jPanel_left = new JPanel();
    private JPanel jPanel_right = new JPanel();

    //二级容器
    private JPanel jp1 = new JPanel();
    private JPanel jp2 = new JPanel();
    private JPanel jp3 = new JPanel();
    private JPanel jp4 = new JPanel();

    //标签
    private JLabel jp1_L = new JLabel();
    private JLabel jp2_L = new JLabel();
    private JLabel jp3_L = new JLabel();

    //文本框
    private JTextField jp1_T = new JTextField();
    //密码框
    private JPasswordField jp2_P = new JPasswordField();
    //复选框
    private JComboBox jp3_C = new JComboBox();

    //按钮
    private JButton jp4_B = new JButton();
    //</editor-fold>

    public void initLogin(){

        //一级容器
        jPanel_main.setLayout(new GridLayout(6,1));
        add(jPanel_main,"Center");

        //占位
        //<editor-fold> defaultstate="collapsed"
        jPanel_left.setLayout(new GridLayout(1,10));
        add(jPanel_left,"West");
        jPanel_left.add(new JPanel());
        jPanel_left.add(new JPanel());
        jPanel_left.add(new JPanel());
        jPanel_left.add(new JPanel());
        jPanel_left.add(new JPanel());
        jPanel_left.add(new JPanel());
        jPanel_left.add(new JPanel());
        jPanel_left.add(new JPanel());
        jPanel_left.add(new JPanel());
        jPanel_left.add(new JPanel());

        jPanel_right.setLayout(new GridLayout(1,10));
        add(jPanel_right,"East");
        jPanel_right.add(new JPanel());
        jPanel_right.add(new JPanel());
        jPanel_right.add(new JPanel());
        jPanel_right.add(new JPanel());
        jPanel_right.add(new JPanel());
        jPanel_right.add(new JPanel());
        jPanel_right.add(new JPanel());
        jPanel_right.add(new JPanel());
        jPanel_right.add(new JPanel());
        jPanel_right.add(new JPanel());
        //</editor-fold>

        //二级容器
        //<editor-fold> defaultstate="collapsed"
        jp1.setLayout(new GridLayout(1,2));
        jPanel_main.add(new JLabel(""));
        jPanel_main.add(jp1);

        jp2.setLayout(new GridLayout(1,2));
        jPanel_main.add(jp2);

        jp3.setLayout(new GridLayout(1,2));
        jPanel_main.add(jp3);

        jp4.setLayout(new GridLayout(1,1));
        jPanel_main.add(jp4);
        jPanel_main.add(new JLabel(""));
        //</editor-fold>

        //内容
        //<editor-fold> defaultstate="collapsed"
        //标签
        jp1_L.setText("账号");
        jp1_L.setFont(new Font("华文中宋",Font.BOLD,18));
        jp1.add(jp1_L);

        jp2_L.setText("密码");
        jp2_L.setFont(new Font("华文中宋",Font.BOLD,18));
        jp2.add(jp2_L);

        jp3_L.setText("身份");
        jp3_L.setFont(new Font("华文中宋",Font.BOLD,18));
        jp3.add(jp3_L);

        //文本框
        jp1.add(jp1_T);

        jp2.add(jp2_P);

        jp3_C.addItem("请选择");
        jp3_C.addItem("学生");
        jp3_C.addItem("班长");
        jp3_C.addItem("教师");
        jp3_C.addItem("管理员");
        jp3.add(jp3_C);

        //按钮
        jp4_B.setText("登录");
        jp4.add(new JLabel(""));
        jp4.add(jp4_B);
        jp4.add(new JLabel(""));

        //</editor-fold>

        //事件
        //<editor-fold> defaultstate="collapsed"
//复选框事件
        final String[] identity = new String[1];
        jp3_C.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                identity[0] = (String) e.getItem();
            }
        });
//按钮事件
        jp4_B.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection con=null;  //连接对象
                Statement stm=null;  //命令对象
                ResultSet rs=null;   //结果集对象
                String name=null;
                if (identity[0].equals("管理员")) {
                    try {
                        Class.forName("com.mysql.jdbc.Driver");
                        String url = "jdbc:mysql://localhost:3306/student?" +
                                "useSSL=true&useUnicode=true&characterEncoding=utf-8";
                        String username = "root";
                        String password = "root";
                        con = DriverManager.getConnection(url, username, password);
                        stm = con.createStatement();
                        rs = stm.executeQuery("select * from user");
                        boolean flag=true;
                        while (rs.next()) {
                            if (jp1_T.getText().equals(rs.getString("number"))) {
                                String s = String.valueOf(jp2_P.getPassword());
                                if (s.equals(rs.getString("password"))) {
                                    setVisible(false);
                                    UserView userView = new UserView();
                                    userView.setVisible(true);
                                    userView.setResizable(false);
                                    userView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                    flag=false;
                                    break;
                                }
                                else{
                                    JOptionPane.showMessageDialog(null,"密码错误");
                                    flag=false;
                                    break;
                                }
                            }
                        }
                        if(!rs.next()&&flag)
                            JOptionPane.showMessageDialog(null,"不存在该管理员");
                        rs.close();
                        stm.close();
                        con.close();
                    } catch (Exception ee) {
                        ee.printStackTrace();
                    }
                }else{
                    try {
                        Class.forName("com.mysql.jdbc.Driver");
                        String url = "jdbc:mysql://localhost:3306/student?"+
                                "useSSL=true&useUnicode=true&characterEncoding=utf-8";
                        String username = "root";
                        String password = "root";
                        con = DriverManager.getConnection(url, username, password);
                        stm = con.createStatement();
                        rs = stm.executeQuery("select * from person");
                        boolean flag=true;
                        while (rs.next()){
                            if (jp1_T.getText().equals(rs.getString("number"))) {
                                String s = String.valueOf(jp2_P.getPassword());
                                if (s.equals(rs.getString("password"))) {
                                    if (identity[0].equals(rs.getString("identity"))) {
                                        if (identity[0].equals("学生")) {
                                            setVisible(false);
                                            StudentView studentView = new StudentView(jp1_T.getText());
                                            studentView.setVisible(true);
                                            studentView.setResizable(false);
                                            studentView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                            flag=false;
                                            break;
                                        } else if (identity[0].equals("班长")) {
                                            setVisible(false);
                                            MonitorView monitorView = new MonitorView(jp1_T.getText());
                                            monitorView.setVisible(true);
                                            monitorView.setResizable(false);
                                            monitorView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                            flag=false;
                                            break;
                                        } else if (identity[0].equals("教师")) {
                                            setVisible(false);
                                            TeacherView teacherView = new TeacherView(jp1_T.getText());
                                            teacherView.setVisible(true);
                                            teacherView.setResizable(false);
                                            teacherView.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                            flag=false;
                                            break;
                                        }
                                    }else if(identity[0].equals("请选择")){
                                        JOptionPane.showMessageDialog(null, "请选择你的身份");
                                        flag=false;
                                        break;
                                    }else {
                                        JOptionPane.showMessageDialog(null, "你不是" + identity[0]);
                                        flag=false;
                                        break;
                                    }
                                }else {
                                    JOptionPane.showMessageDialog(null, "密码错误");
                                    flag=false;
                                    break;
                                }
                            }
                        }
                        if(!rs.next()&&flag)
                            JOptionPane.showMessageDialog(null,"账号不存在");
                        rs.close();
                        stm.close();
                        con.close();
                    } catch (Exception ee) {
                        ee.printStackTrace();
                    }
                }

            }
        });
        //</editor-fold>
    }

}
